package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.webkit.WebView;

import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.DefaultWebHandler;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class YhouseWebHandler extends DefaultWebHandler {

    public YhouseWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what){
            case IWebType.TYPE_ON_PAGE_FINISHED:
                setYhouseTitle(msg);
                break;
        }
    }

    public void setYhouseTitle(Message msg){
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            WebView webview = mActivity.getWebview();
            TopLayout topLayout = mActivity.getTop_layout();
            String url = (String) msg.obj;
            setYhouseTitle(webview,topLayout,url);
        }
    }

    public void setYhouseTitle(WebView view, TopLayout topLayout, String linkUrl) {
        if (view != null) {
            String title = view.getTitle();
            if (linkUrl.contains("yhouse")) {
                if (!TextUtils.isEmpty(title) && title.contains("-")) {
                    title = title.substring(0, title.indexOf("-"));
                }
            }
            if (!TextUtils.isEmpty(title))
                topLayout.setTopTitle(title);
        }
    }

}
